//自己实现有泛型的栈

import java.util.Arrays;

public class MyStack2<E> {
    public Object[] elem;
    public int usedSize;

    //栈的构造方法
    public MyStack2(){
        this.elem = new Object[10];
    }

    //压栈
    public void push(E val){
        if (isFull()) {
            //扩容
            elem = Arrays.copyOf(elem, 2 * elem.length);
        }
            elem[usedSize] = val;
            usedSize ++;
    }

    //出栈
    public E pop(){
        if(empty()){
            return null;
        }
        E oldVal = (E)elem[usedSize - 1];
        usedSize --;
        return oldVal;
    }

    //获取栈顶元素
    public E peek(){
        if (empty()){
            return null;
        }
        return (E)elem[usedSize - 1];
    }

    //判断栈是否满了
    public boolean isFull(){
        return usedSize == elem.length;
    }

    //判断栈是否为空
    public boolean empty(){
        return usedSize == 0;
    }
}
